package com.gc.ordersys2.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gc.ordersys2.common.result.Result;
import com.gc.ordersys2.common.result.ResultUtils;
import com.gc.ordersys2.common.utils.EmptyUtils;
import com.gc.ordersys2.entity.User;
import com.gc.ordersys2.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cuijianhao
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService service;

    @RequestMapping("/getList")
    public Result<User> getList(Integer page, Integer limit) {
        //封装查询条件，排序方式
        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("tname","爆品");
        queryWrapper.orderByDesc("id");

        //分页
        IPage<User> p = new Page<>(page, limit);


        //查询---带条件分页查询
        IPage list = service.page(p, queryWrapper);
        List records = list.getRecords();   //当前页的数据
        int total = (int) list.getTotal();   //总条数

        return ResultUtils.returnSuccessLayui(records, total);
    }

    @RequestMapping("/del")
    public Object del(Integer id) {
        if (service.removeById(id)) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail();
        }
    }

    @RequestMapping("/add")
    public Object add(User user) {
        if (service.save(user)) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail("添加失败");
        }
    }

    @RequestMapping("/update")
    public Object update(User user) {
        if (service.updateById(user)) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail("修改失败");
        }
    }

    @RequestMapping("/login")
    public Object login(User user, HttpSession session) {
        //条件封装
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uname", user.getUname());
        queryWrapper.eq("psw", user.getPsw());
        //查询--根据条件查询单个
        User one = service.getOne(queryWrapper);
        //判断查询的结果
        if (EmptyUtils.isNotEmpty(one)) {
            session.setAttribute("userName", one.getUname());
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail("用户名或密码错误");
        }
    }

    @RequestMapping("/logout")
    public Object logout(User user, HttpSession session) {
        session.removeAttribute("userName");
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/getUser")
    public Object getUser(HttpSession session) {
        Object userName = session.getAttribute("userName");
        if (EmptyUtils.isNotEmpty(userName)) {
            return ResultUtils.returnDataSuccess(userName);
        } else {
            return ResultUtils.returnFail("用户未登录");
        }
    }
}
